package com.bitcoin.scheduler.config;

import com.bitcoin.scheduler.task.*;
//import com.bitcoin.scheduler.task.CurrentPriceTask;
import io.github.jhipster.config.JHipsterConstants;
import org.quartz.*;
import org.springframework.boot.autoconfigure.quartz.SchedulerFactoryBeanCustomizer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.SchedulingConfiguration;

@Configuration
@Profile(JHipsterConstants.SPRING_PROFILE_PRODUCTION)
public class QuartzConfiguration extends SchedulingConfiguration {


    @Bean
    public OpenEntityManagerJobListener openEntityManagerJobListener(ApplicationContext applicationContext) {
        OpenEntityManagerJobListener listener = new OpenEntityManagerJobListener();
        listener.setApplicationContext(applicationContext);
        return listener;
    }

    //调度工厂
    @Bean
    public SchedulerFactoryBeanCustomizer schedulerFactoryBeanCustomizer(OpenEntityManagerJobListener listener) {
        return schedulerFactoryBean -> {
            schedulerFactoryBean.setGlobalJobListeners(listener);
            schedulerFactoryBean.setStartupDelay(3);
        };
    }


    @Bean
    @Profile(JHipsterConstants.SPRING_PROFILE_PRODUCTION)
    public JobDetail[] jobDetails() {
        JobDetail basicInfoJob = JobBuilder.newJob()
            .ofType(BccBasicInfoTask.class)
            .withIdentity("BccBasicInfoTask")
            .storeDurably()
            .build();

        JobDetail priceJob = JobBuilder.newJob()
            .ofType(BccPriceInfoTask.class)
            .withIdentity("BccPriceInfoTask")
            .storeDurably()
            .build();
        JobDetail priceFillJob = JobBuilder.newJob()
            .ofType(BccPriceDetailFillingTask.class)
            .withIdentity("BccPriceDetailFillingTask")
            .storeDurably()
            .build();
        JobDetail bcc24HourVolumeTaskJob = JobBuilder.newJob()
            .ofType(Bcc24HourVolumeTask.class)
            .withIdentity("Bcc24HourVolumeTask")
            .storeDurably()
            .build();

        JobDetail syncBlockccKlineTask = JobBuilder.newJob()
            .ofType(SyncBlockccKlineTask.class)
            .withIdentity("SyncBlockccKlineTask")
            .storeDurably()
            .build();
        return new JobDetail[]{
            basicInfoJob
            , priceJob
            , priceFillJob
            , bcc24HourVolumeTaskJob
            , syncBlockccKlineTask
        };
    }

    @Bean
    @Profile(JHipsterConstants.SPRING_PROFILE_PRODUCTION)
    public Trigger[] triggers() {

        Trigger basicInfoTrigger = TriggerBuilder.newTrigger()
            .forJob("BccBasicInfoTask")
            .withSchedule(CronScheduleBuilder.cronSchedule("0 0/30 * * * ?"))
            .withIdentity("basicInfoTrigger")
            .build();
        Trigger bcc24HourVolumeTaskTrigger = TriggerBuilder.newTrigger()
            .forJob("Bcc24HourVolumeTask")
            .withSchedule(CronScheduleBuilder.cronSchedule("0 0/10 * * * ?"))
            .withIdentity("bcc24HourVolumeTaskTrigger")
            .build();
        Trigger priceTrigger = TriggerBuilder.newTrigger()
            .forJob("BccPriceInfoTask")
            .withSchedule(CronScheduleBuilder.cronSchedule("0 0/1 * * * ?"))
            .withIdentity("priceTrigger")
            .build();
        Trigger priceFillTrigger = TriggerBuilder.newTrigger()
            .forJob("BccPriceDetailFillingTask")
            .withSchedule(CronScheduleBuilder.cronSchedule("0/30 * * * * ?"))
            .withIdentity("priceFillTrigger")
            .build();

        Trigger syncBlockccKlineTrigger = TriggerBuilder.newTrigger()
            .forJob("SyncBlockccKlineTask")
            .withSchedule(CronScheduleBuilder.cronSchedule("0 0/5 * * * ?"))
            .withIdentity("syncBlockccKlineTrigger")
            .build();
        return new Trigger[]{
            basicInfoTrigger
            , priceTrigger
            , priceFillTrigger
            , bcc24HourVolumeTaskTrigger
            ,syncBlockccKlineTrigger
        };
//        return new Trigger[]{bcc24HourVolumeTaskTrigger};
    }
}
